<template>
  <div class="home">
    <v-header mine />
    <div class="content">
      <div class="banner">
        <swiper ref="mySwiper" :options="swiperOptions">
          <swiper-slide>
            <a href="#/notice">
              <img class="swiper-slide-img" src="../assets/images/diy-ban.jpg" alt="" />
            </a>
          </swiper-slide>
          <swiper-slide v-for="item in banner" :key="item.img">
            <img class="swiper-slide-img" :src="item.img" alt="" />
          </swiper-slide>
          <div class="swiper-pagination" slot="pagination"></div>
        </swiper>
      </div>
      <div class="list">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell class="company-item" v-for="item in jobList" :key="item.id">
              <div class="company-info clearfix">
                <div class="left fl">
                  <p class="above name">{{ item.outerName }}</p>
                  <div class="below">
                    <span class="tip">{{ item.code }}</span>
                    <span class="tip">{{ item.properDes }}</span>
                    <span class="tip">{{ item.peopleCnt }}</span>
                  </div>
                </div>
                <div class="right fr">
                  <p class="above name">{{ item.jobCnt }}个</p>
                  <div class="below">在招岗位</div>
                </div>
              </div>
              <div class="job-list" :class="item.moreState ? 'active' : ''">
                <div class="job-list-box">
                  <div
                    class="job-list-item"
                    v-for="(elem, index) in item.data"
                    :key="elem.id"
                    @click="checkJobDetail(item, elem)"
                  >
                    <div class="job-name clearfix">
                      <!-- <div class="fl index"></div> -->
                      <div class="fl name">{{ index + 1 }} {{ elem.name }}</div>
                      <div class="fr money">{{ elem.salary }}</div>
                    </div>
                    <div class="job-needs clearfix">
                      <div class="job-needs-item fl icon-time">
                        {{ elem.workerTypeDes || '全职' }}
                      </div>
                      <div class="job-needs-item fl icon-sex">
                        {{ elem.genderDes || '不限' }}
                      </div>
                      <div class="job-needs-item fl icon-age">{{ elem.ageRequire || '不限' }}</div>
                      <div class="job-needs-item fl icon-xx">
                        {{ elem.educationDes || '不限' }}
                      </div>
                      <div class="job-needs-item fl icon-bag">
                        {{ elem.experienceDes || '不限' }}
                      </div>
                    </div>
                    <div class="updatetime">{{ elem.updateTimeDes }}</div>
                  </div>
                </div>
                <!-- active 收起 -->
                <div class="load-more" v-show="item.showMore" @click="showMore(item)">
                  <span class="word">{{ item.moreState ? '收起' : '展开' }}</span>
                </div>
              </div>
            </van-cell>
          </van-list>
        </van-pull-refresh>
      </div>
    </div>
    <div class="mask" v-if="showDetail" @click="showDetail = false"></div>
    <div class="dialog-box" v-if="showDetail">
      <div class="dialog-content">
        <div class="detail">
          <div class="detail-item clearfix">
            <div class="name fl">企业编号：</div>
            <div class="value fl">{{ showDetailInfo.code }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">岗位名称：</div>
            <div class="value fl">{{ showDetailInfo.name }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">岗位薪资：</div>
            <div class="value fl">{{ showDetailInfo.salary }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">用工类型：</div>
            <div class="value fl">{{ showDetailInfo.workerTypeDes }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">性别要求：</div>
            <div class="value fl">{{ showDetailInfo.genderDes }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">年龄要求：</div>
            <div class="value fl">{{ showDetailInfo.ageRequire }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">学历要求：</div>
            <div class="value fl">{{ showDetailInfo.educationDes }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">经验要求：</div>
            <div class="value fl">{{ showDetailInfo.experienceDes }}</div>
          </div>
          <div class="detail-item clearfix">
            <div class="name fl">其他要求：</div>
            <div class="value fl">{{ showDetailInfo.otherRequire }}</div>
          </div>
        </div>
        <div class="tip">{{ tooltipConcent }}</div>
        <div class="close-dialog" @click="showDetail = false">
          <van-icon name="cross" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import Header from '@/components/header.vue'

const img1 = require('../assets/images/banner1.png')
const img2 = require('../assets/images/banner2.png')

export default {
  name: 'Home',
  data() {
    return {
      banner: [{ img: img1 }, { img: img2 }],
      jobList: [],
      loading: false,
      finished: false,
      refreshing: false,
      showDetail: false,
      showDetailInfo: {},
      swiperOptions: {
        // notNextTick: true,
        loop: true,
        autoplay: {
          disableOnInteraction: false,
          delay: 5000
        },
        direction: 'horizontal',
        autoHeight: true,
        pagination: {
          el: '.swiper-pagination'
        }
      },
      paginationParams: {
        pageNo: 1,
        pageSize: 10
      },
      tooltipConcent: '如有求职意向,请联系：' + this.$config.contactPhone
    }
  },
  computed: {},
  mounted() {
    this.$http.getBannerList().then((res) => {
      if (res.success) {
        this.banner = res.model
      }
    })
  },
  methods: {
    onLoad() {
      this.$http.getJobList(this.paginationParams).then((res) => {
        if (res.success) {
          if (this.refreshing) {
            this.jobList = []
            this.refreshing = false
          }
          if (res.model.data.length > 0) {
            let result = res.model.data
            result = result.map((item) => {
              if (item.data && item.data.length > 3) {
                item.showMore = true
                item.moreState = false
              }
              return item
            })
            this.paginationParams.pageNo++
            this.loading = false
            this.jobList = this.jobList.concat(result)
          }
          if (this.jobList.length >= res.model.totalCount) {
            this.finished = true
          }
        }
      })
    },
    onRefresh() {
      this.paginationParams.pageNo = 1
      this.finished = false
      this.loading = true
      this.onLoad()
    },
    showMore(item) {
      item.moreState = !item.moreState
    },
    checkJobDetail(item, elem) {
      this.showDetail = true
      this.showDetailInfo.code = item.code
      this.showDetailInfo.name = elem.name
      this.showDetailInfo.salary = elem.salary
      this.showDetailInfo.workerTypeDes = elem.workerTypeDes
      this.showDetailInfo.genderDes = elem.genderDes
      this.showDetailInfo.ageRequire = elem.ageRequire
      this.showDetailInfo.educationDes = elem.educationDes
      this.showDetailInfo.experienceDes = elem.experienceDes
      this.showDetailInfo.otherRequire = elem.otherRequire
    }
  },
  components: {
    'v-header': Header,
    Swiper,
    SwiperSlide
  }
}
</script>

<style lang="less">
.swiper-slide-img {
  width: 100%;
  height: 3rem;
}
.home {
  .content {
    padding-bottom: 1rem;
    .list {
      padding: 0.2rem 0.2rem;
      .company-item {
        border-radius: 0.12rem;
        border: 1px solid #ddd;
        margin-bottom: 0.2rem;
        .company-info {
          padding: 0.2rem 0.2rem;
          background: #fbfbfb;
          border-bottom: 1px solid #ddd;
          .left {
            max-width: 80%;
            text-align: left;
          }
          .above {
            color: #333;
            font-size: 0.32rem;
            margin-bottom: 0.2rem;
            height: 24px;
            line-height: 24px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }
          .right {
            text-align: right;
          }
          .below {
            color: #666;
            font-size: 0.24rem;
            height: 0.24rem;
            line-height: 0.3rem;
            text-align: left;
            transform: translateY(-2px);
            .tip {
              display: inline-block;
              border-left: 1px solid #666;
              padding: 0 0.05rem;
            }
            .tip:first-child {
              border: none;
            }
          }
        }
        .job-list {
          padding: 0 0.2rem;
          .job-list-box {
            max-height: 8.12rem;
            overflow: hidden;
            transition: all 0.1s linear;
          }
          .job-list-item {
            height: 2.7rem;
            padding: 0.3rem 0;
            border-bottom: 1px solid #ddd;
            box-sizing: border-box;
            .job-name {
              font-size: 0.32rem;
              color: #333;
              height: 0.4rem;
              line-height: 0.4rem;
              // .index {
              //   height: 0.4rem;
              //   line-height: 0.4rem;
              //   margin-right: 0.1rem;
              // }
              .money {
                height: 0.4rem;
                line-height: 0.4rem;
                color: #f60;
                width: 40%;
                overflow: hidden;
                // text-overflow: ellipsis;
                white-space: nowrap;
                text-align: right;
              }
              .name {
                height: 0.4rem;
                line-height: 0.4rem;
                width: 60%;
                overflow: hidden;
                // text-overflow: ellipsis;
                white-space: nowrap;
              }
            }
            .job-needs {
              margin: 0.2rem 0;
              .job-needs-item {
                width: 33.3%;
                height: 0.4rem;
                padding-left: 0.8rem;
                text-align: left;
                line-height: 0.42rem;
                overflow: hidden;
                margin: 0.05rem 0;
                color: #888;
              }
              .icon-time {
                background: url('../assets/images/time.svg') no-repeat 0.3rem center;
                background-size: 0.4rem;
              }
              .icon-sex {
                background: url('../assets/images/sex.svg') no-repeat 0.3rem center;
                background-size: 0.36rem;
              }
              .icon-age {
                background: url('../assets/images/me.svg') no-repeat 0.3rem center;
                background-size: 0.4rem;
              }
              .icon-xx {
                background: url('../assets/images/xx.svg') no-repeat 0.3rem center;
                background-size: 0.4rem;
              }
              .icon-bag {
                background: url('../assets/images/bag.svg') no-repeat 0.3rem center;
                background-size: 0.38rem;
              }
            }
            .updatetime {
              color: #666;
              font-size: 0.26rem;
              text-align: left;
              margin-left: 0.3rem;
              height: 0.3rem;
              line-height: 0.3rem;
            }
          }
          .job-list-item:last-child {
            border-bottom: none;
          }
          .load-more {
            height: 0.98rem;
            line-height: 1rem;
            color: #888;
            text-align: center;
            .word {
              display: inline-block;
              padding-right: 0.5rem;
              background: url('../assets/images/down.png') no-repeat right center;
              background-size: 0.4rem;
              transition: all 0.2s linear;
            }
          }
        }
        .job-list.active {
          .job-list-box {
            max-height: 100% !important;
          }
          .load-more {
            .word {
              background: url('../assets/images/up.png') no-repeat right center;
              background-size: 0.4rem;
            }
          }
        }
      }
    }
  }
}
.mask {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 99;
}
.dialog-box {
  width: 90%;
  position: fixed;
  top: 50%;
  left: 5%;
  z-index: 100;
  background: #fff;
  transform: translateY(-50%);
  border-radius: 6px;
  .dialog-content {
    .detail {
      padding: 0.4rem 0.4rem;
      font-size: 0.26rem;
      .detail-item {
        line-height: 0.44rem;
        margin: 0.2rem;
        .name {
          width: 26%;
          text-align: justify;
        }
        .value {
          width: 74%;
          text-align: left;
        }
      }
    }
    .tip {
      text-align: center;
      height: 0.98rem;
      line-height: 0.98rem;
      border-top: 1px solid #ddd;
    }
    .close-dialog {
      width: 0.6rem;
      height: 0.6rem;
      font-size: 24px;
      position: absolute;
      right: 0.2rem;
      top: 0.2rem;
      .van-icon {
        transform: translateY(0.08rem);
      }
    }
  }
}
</style>
